package com.fuego.creditsystemnews.controller;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fuego.creditsystemnews.service.INewsService;
import com.fuego.creditsystemuser.service.IUserService;
import jakarta.annotation.Resource;
import org.example.dto.Result;
import org.example.entity.News;
import org.example.entity.User;
import org.example.utils.SystemConstants;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@RestController
@RequestMapping("/news")
public class NewsController {

    @Resource
    private INewsService newsService;



    @PostMapping("/like/{id}")
    public Result likeNews(@PathVariable("id") Long id, @RequestParam("userId") Long userId) {
        return newsService.likeNews(id,userId);
    }

    @Resource
    private IUserService userService;

    @GetMapping("/of/me")
    public Result queryMyNews(@RequestParam(value = "current", defaultValue = "1") Integer current, @RequestParam("userId") Long userId) {
        // 获取登录用户
        User user = userService.getById(userId);
        // 根据用户查询
        Page<News> page = newsService.query()
                .eq("user_id", user.getId()).page(new Page<>(current, SystemConstants.MAX_PAGE_SIZE));
        // 获取当前页数据
        List<News> records = page.getRecords();
        return Result.ok(records);
    }

    @GetMapping("/hot")
    public Result queryHotNews(@RequestParam(value = "current", defaultValue = "1") Integer current, @RequestParam("userId") Long userId) {
        return newsService.queryHotNews(current,userId);
    }

    @GetMapping("/news-detail/{id}")
    public Result queryNewsById(@PathVariable("id") Long id, @RequestParam("userId") Long userId) {
        return newsService.queryNewsById(id,userId);
    }


    @GetMapping("/of/user")
    public Result queryNewsByUserId(
            @RequestParam(value = "current", defaultValue = "1") Integer current,
            @RequestParam("id") Long userId) {

        return newsService.queryNewsByUserId(current,userId);
    }


    @GetMapping("/of/follow")
    public Result queryNewsOfFollow(
            @RequestParam("lastId") Long max,@RequestParam(value = "offset",defaultValue = "0") Integer offset, @RequestParam("userId") Long userId){
        return newsService.queryNewsOfFollow(max,offset,userId);
    }

}